"""
@description: Insert into the database when events occur.
@author: Zzay
@create: 2022/07/11
"""
import datetime
import argparse

# constants
CONFIG_FILE_PATH = "allow_insert_database.txt"

# read config file
file = open(CONFIG_FILE_PATH, "r")
contents = file.read()
file.close()

# get "is_allowed" attribute
is_allowed = contents[11:12]
if is_allowed == "1":
    # if insertion to the database is currently allowed
    # update status "is_allowed"
    file = open(CONFIG_FILE_PATH, "w")
    file.write("is_allowed=0")
    file.close()

    print("[INFO] Ready to insert new event into the database...")

    # input parameters
    ap = argparse.ArgumentParser()
    ap.add_argument("-ed", "--event_desc", required=False,
                    default='', help="")
    ap.add_argument("-el", "--event_location", required=False,
                    default='', help="")
    ap.add_argument("-et", "--event_type", required=False,
                    default='', help="")
    ap.add_argument("-epi", "--elderly_person_id", required=False,
                    default='', help="")
    args = vars(ap.parse_args())

    # assign values according to input parameters
    event_desc = args['event_desc']
    event_location = args['event_location']
    event_type = int(args['event_type']) if args['event_type'] else None
    elderly_person_id = int(args['elderly_person_id']) if args['elderly_person_id'] else None

    # time of the event
    event_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    # request payload
    payload = {
        'id': 0,  # id=0 means insert; id=1 means update;
        'event_desc': event_desc,
        'event_type': event_type,
        'event_date': event_date,
        'event_location': event_location,
        'elderly_person_id': elderly_person_id
    }

    print('调用插入事件数据的API')
    print('插入成功')
else:
    # if insertion to the database is currently not allowed
    print('just pass')
